<template>
  <el-dropdown class="user-avatar-dropdown" @command="handleCommand" trigger="click">
    <div class="avatar-container">
      <div class="avatar-placeholder" v-if="!userInfo.avatar">
        {{ userInfo.username ? userInfo.username.substring(0, 1).toUpperCase() : 'U' }}
      </div>
      <el-avatar :size="32" :src="userInfo.avatar" v-else></el-avatar>
      <div class="user-info">
        <span class="username">{{ userInfo.username || '未登录' }}</span>
        <i class="el-icon-arrow-down"></i>
      </div>
    </div>

    <el-dropdown-menu slot="dropdown" class="user-dropdown-menu">
      <div class="dropdown-header">
        <div class="user-role">{{ userInfo.role || '普通用户' }}</div>
        <div class="user-email">{{ userInfo.email || '未设置邮箱' }}</div>
      </div>
      <div class="dropdown-divider"></div>
      <el-dropdown-item command="userCenter">
        <i class="el-icon-user"></i>个人中心
      </el-dropdown-item>
      <div class="dropdown-divider"></div>
      <el-dropdown-item command="loginOut">
        <i class="el-icon-switch-button"></i>退出登录
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
export default {
  inject: ['userInfo'],
  name: 'UserAvatar',
  data() {
    return {
      imgBaseUrl: this.HOST,
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'userCenter') {
        this.$router.push({ path: '/personInfo' })
      } else if (command === 'settings') {
        this.$router.push({ path: '/settings/account' })
      } else if (command === 'messages') {
        this.$router.push({ path: '/messages' })
      } else if (command === 'loginOut') {
        this.loginOut()
      }
    },
    loginOut() {
      this.$confirm('确定要退出系统吗？', '退出提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        localStorage.removeItem("backUser")
        this.$router.push({ path: '/login'});
      }).catch(() => {
        // 取消退出
      })
    }
  }
}
</script>

<style lang="less" scoped>
.user-avatar-dropdown {
  height: 100%;
  display: flex;
  align-items: center;
  cursor: pointer;
  
  .avatar-container {
    display: flex;
    align-items: center;
    padding: 0 8px;
    height: 40px;
    border-radius: 20px;
    transition: all 0.3s;
    
    &:hover {
      background-color: rgba(255, 255, 255, 0.2);
    }
  }
  
  .avatar-placeholder {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background-color: #4caf50;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: bold;
  }
  
  .user-info {
    margin-left: 8px;
    display: flex;
    align-items: center;
  }
  
  .username {
    font-size: 14px;
    color: #ffffff;
    margin-right: 4px;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  i {
    font-size: 12px;
    color: #ffffff;
  }
}

.user-dropdown-menu {
  width: 220px;
  padding: 0;
  margin-top: 5px;
  
  .dropdown-header {
    padding: 15px;
    background-color: #f5f7fa;
    border-top-left-radius: 4px;
    border-top-right-radius: 4px;
    
    .user-role {
      font-size: 12px;
      color: #909399;
      margin-bottom: 4px;
    }
    
    .user-email {
      font-size: 13px;
      color: #606266;
      word-break: break-all;
    }
  }
  
  .dropdown-divider {
    height: 1px;
    background-color: #ebeef5;
    margin: 0;
  }
  
  .el-dropdown-menu__item {
    display: flex;
    align-items: center;
    padding: 12px 15px;
    font-size: 14px;
    
    i {
      font-size: 16px;
      margin-right: 8px;
      color: #606266;
    }
    
    &:hover i {
      color: #4caf50;
    }
  }
  
  .message-badge {
    margin-left: auto;
  }
}
</style>